﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace DirectOPCClient
{
    public partial class DataForm : Form
    {
        public static ConcurrentDictionary<string, string> Datacache = new ConcurrentDictionary<string, string>();  //数据项和值
       
        private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer() { Interval = 2000 };

        public DataForm()
        {
            InitializeComponent();
        }

        private void DataForm_Load(object sender, EventArgs e)
        {
            
            Datacache = MainForm.cache;
            //建一个线程显示采集数据
           
            timer1.Tick += new EventHandler(this.ShowDate);
            
            this.timer1.Enabled = true;
            //设置Timer1 时间间隔
            this.timer1.Interval = 2000;

            timer1.Start();
            // Thread ShowDataThread = new Thread(ShowDate);
            // ShowDataThread.Start();
        }

        private void ShowDate(object obj,EventArgs e)
        {
            listView1.Items.Clear();

            foreach (var item in Datacache)
                {
                   
                    ListViewItem item1 = new ListViewItem(item.Key);
                    item1.SubItems.Add(item.Value);
                    listView1.Items.Add(item1);
                    /*this.Invoke(new Action<ListViewItem>((a) =>
                    {
                        listView1.Items.Add(a);
                    }), new object[] { item1 });*/
                }
           
        }

        private void CloseForm(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
           
        }
    }
}
